import os


'''
this method can detect  fileencoding only if
the codepage belongs to
ascii，ascii_bom,utf8,utf8_bom,cp936

'''
def detect(binary_data):
    data = binary_data
    try:
        data.decode('utf8')
        try:
            data.decode('utf8').encode('cp936')
            try:
                data.decode('cp936').encode('utf8')
                return 'ascii'

            except:
                return 'utf8'
        except:
            return 'utf8-bom'
    except:
        return 'cp936'


if __name__ == '__main__':
    for f in [x for x in os.listdir() if 'bom' in x]:
        with open(f, 'rb') as fp:
            data = fp.read()

        print('{0}:\t {1}'.format(f.rjust(21),detect(data)))
